<?php

namespace App\Trip\Plane\Variflight\Result;

use App\Jobs\Plane\ProcessComfortData;
use App\Models\Plane\Flight\FlightCabin;
use App\Trip\Plane\Cabin\CabinClass;
use Exception;

class QueryResultFlight
{
    private array $flight;

    public function __construct(array $flight)
    {
        $this->flight = $flight;
        ProcessComfortData::dispatch($this);
    }

    public function getFlightNo()
    {
        return $this->FlightNo;
    }

    public function getFlightInfo(): array
    {
        $flight_info           = [
            'flight_year'           => $this->FlightYear,
            'flight_no'             => $this->FlightNo,
            'trip_date'             => $this->FlightDate,
            'from_code'             => $this->FlightDepcode,
            'to_code'               => $this->FlightArrcode,
            'cancel_rate'           => $this->CancelProb,// 取消率
            'from_punctuality_rate' => $this->OntimeRate,// 出发准点率
            'to_punctuality_rate'   => $this->ArrOntimeRate,// 到达准点率
        ];
        $flight_info['cabins'] = $this->getCabins();
        return $flight_info;
    }

    public function getCabins(): array
    {
        $ec    = CabinClass::ECONOMY;
        $first = CabinClass::FIRST;
        $fc    = new FlightCabin();


        try {
            $flight_cabin = $fc->findByFlightAndCabin($this->FlightNo, $this->Cabin);
            $default      = $flight_cabin?->typeDesc() == $first ? $first : $ec;
        } catch (Exception $exception) {
            $default = $ec;
        }
        $defaultIsEc = $default == $ec;

        $ret[] = [
            "cabin_name"    => $default,
            "cabin_name_uy" => trans2uy($default),
            "cabin"         => $this->Cabin,
            'seat_space'    => __($this->SeatSpace),// 位置
            'seat_width'    => __($this->SeatWidth),// 宽度
            'seat_tilt'     => __($this->SeatSpace),// 倾斜度
            'meal'          => $this->Food == '有餐食' ? 1 : 0,// 有无餐食
            'meal_type'     => __($this->FoodCode ?: $this->Food),//  餐食类型
            'has_media'     => ($this->Media && $this->Media != '无') ? 1 : 0,// 有无多媒体
            'media'         => __($this->Media),// 有无餐食
            'has_power'     => in_array($this->Socket, ['有', '电源+USB', 'USB']) ? 1 : 0,// 有无电源
            'power'         => __($this->Socket),// 电源类型
            'has_wifi'      => $this->WiFi == '有' ? 1 : 0,// 有无电源
            'wifi'          => __($this->WiFi),// 有无餐食
        ];

        if ($this->SubCabin) {
            $cabin_name = $defaultIsEc ? $first : $ec;
            $type       = $defaultIsEc ? CabinClass::FIRST_CODE : CabinClass::ECONOMY_CODE;
            // 非default 的舱位
            $cabin_codes = collect($fc->listByFlight($this->FlightNo))->where('type', $type)->pluck('cabin')->unique()->toArray();
            $ret[]       = collect($this->SubCabin)
                ->where('Cabin', '!=', $this->Cabin)
                ->whereIn('Cabin', $cabin_codes)
                ->map(fn($item) => [
                    "cabin_name"    => $cabin_name,
                    "cabin_name_uy" => trans2uy($cabin_name),
                    "cabin"         => $item['Cabin'],
                    'seat_space'    => __($item['SeatSpace']),// 位置
                    'seat_width'    => __($item['SeatWidth']),// 宽度
                    'seat_tilt'     => __($item['SeatSpace']),// 倾斜度
                    'meal'          => $item['Food'] == '有餐食' ? 1 : 0,// 有无餐食
                    'meal_type'     => __($item['FoodCode'] ?? $item['Food']),//  餐食类型 FoodCode ...没有...
                    'has_media'     => ($item['Media'] && $item['Media'] != '无') ? 1 : 0,// 有无多媒体
                    'media'         => __($item['Media']),// 多媒体
                    'has_power'     => in_array($item['Socket'], ['有', '电源+USB', 'USB']) ? 1 : 0,// 有无电源
                    'power'         => __($item['Socket']),// 电源类型
                    'has_wifi'      => $item['WiFi'] == '有' ? 1 : 0,// 有无wifi
                    'wifi'          => __($item['WiFi']),// wifi
                ])->first();
        }
        return collect($ret)->filter(fn($v) => $v['cabin'] ?? false)->toArray();
    }

    public function __get(string $name)
    {
        return $this->flight[$name] ?? '';
    }
}